package org.baicaixiaozhan.tenet.script;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Optional;

/**
 * DESC: 命令行脚本执行器默认实现
 *
 * @author baicaixiaozhan
 * @since 1.0.0
 */
public class DefaultCommandScriptExecutor implements CommandScriptExecutor {

    @Override
    public Optional<Object> execute(String command, String... args) {
        Process proc;
        try {
            proc = Runtime.getRuntime().exec(command + " " + String.join(" ", args));
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        return Optional.empty();
    }

}
